import 'package:flutter/material.dart';
import 'package:get/get.dart';

class LoadingDialog {
  static late BuildContext _context;

  static void show({
    String loadingText = '加载中',
    bool showLoadingText = true,
  }) {
    _context = Get.context!;
    Navigator.push(
      _context,
      DialogRouter(
        UiLoadingDialog(
          loadingText: loadingText,
          showLoadingText: showLoadingText,
        ),
      ),
    );
  }

  static void hidden() {
    if (Navigator.canPop(_context)) {
      Navigator.pop(_context);
    }
  }
}

class DialogRouter extends PageRouteBuilder {
  final Widget page;

  DialogRouter(this.page)
      : super(
          opaque: false,
          pageBuilder: (context, animation, secondaryAnimation) => page,
        );
}

class UiLoadingDialog extends StatelessWidget {
  const UiLoadingDialog({
    super.key,
    this.loadingText = '加载中',
    this.showLoadingText = true,
  });
  final String loadingText;
  final bool showLoadingText;
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Material(
        ///背景透明
        color: Colors.black54,

        ///保证控件居中效果
        child: Center(
          ///弹框大小
          child: SizedBox(
            width: 120.0,
            height: 120.0,
            child: Container(
              ///弹框背景和圆角
              decoration: const ShapeDecoration(
                color: Color(0xffffffff),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(
                    Radius.circular(8.0),
                  ),
                ),
              ),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  const CircularProgressIndicator(),
                  if (showLoadingText)
                    Padding(
                      padding: const EdgeInsets.only(
                        top: 20.0,
                      ),
                      child: Text(
                        loadingText,
                        style: const TextStyle(fontSize: 16.0),
                      ),
                    ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
